package online.yysh.charging;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.Observer;


import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.Login;
import online.yysh.charging.bean.request.ApplyBackRequest;
import online.yysh.charging.inputfilter.CashierInputFilter;
import online.yysh.charging.livedata.UserManager;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class WithdrawalActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_cancel,tv_diy,tv_title,tv_balance;
    private EditText et_money;
    private ImageView iv_show;

    private boolean showStatus = true;
    private String myMoney;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_balance = findViewById(R.id.tv_balance);
        iv_show = findViewById(R.id.iv_show);
        iv_show.setOnClickListener(this);
        et_money = findViewById(R.id.et_money);
        et_money.setFilters(new InputFilter[]{new CashierInputFilter()});
        tv_title = findViewById(R.id.tv_title);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
        tv_diy = findViewById(R.id.tv_diy);
        tv_diy.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tv_title.setText(R.string.withdrawal);
        tv_diy.setText(R.string.withdrawal_history);
        UserManager.getInstance().getLiveData().observe(this, new Observer<Login>() {
            @Override
            public void onChanged(Login login) {
                myMoney = "账户余额："+login.getMoney()+"元";
                tv_balance.setText(myMoney);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.iv_show:
                if (showStatus){
                    tv_balance.setText("账户余额：********");
                    showStatus = false;
                    iv_show.setBackgroundResource(R.mipmap.invisible);
                }else{
                    tv_balance.setText(myMoney);
                    showStatus = true;
                    iv_show.setBackgroundResource(R.mipmap.visible);
                }
                break;
            case R.id.tv_diy:
                //进入提现记录页面
                startActivity(WithdrawalRecordActivity.class);
                break;
        }
    }

    public void applyBack(View view){
        String money = et_money.getText().toString().trim();
        if (TextUtils.isEmpty(money)){
            showToast("请输入正确的提现金额");
            return;
        }
        final ApplyBackRequest request = new ApplyBackRequest(openid,"",Double.valueOf(money));
        HttpUtil.getInstance().applyBack(request, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                showToast("申请提交失败，请重新申请");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                logE("applyBack:"+result);
                if (result.contains("ok")){
                    showToast("申请已提交，请等待商家确认");
                    finish();
                }else{
                    showToast("申请提交失败，请重新申请");
                }
            }
        });
    }
}
